package com.woniuxy.service.impl;

import com.woniuxy.dao.CampRepo;
import com.woniuxy.entity.Camp;
import com.woniuxy.entity.City;
import com.woniuxy.mapper.CampMapper;
import com.woniuxy.mapper.CityMapper;
import com.woniuxy.service.CampService;
import com.woniuxy.vo.CampVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class CampServieImpl implements CampService {
    @Resource
    private CampMapper campMapper;
    @Resource
    private CityMapper cityMapper;
    @Resource
    private CampRepo campRepo;
    @Override
    public List<CampVO> pageSelectCampByExample(Map<String, Object> map) throws Exception {
        return campMapper.pageSelectCampByExample(map);
    }

    @Override
    public int countCampByExample(Map<String, Object> map) throws Exception {
        return campMapper.countCampByExample(map);
    }

    @Override
    public int updateCamp(Camp camp) throws Exception {
        //更新前elasticsearch也对应更新
        CampVO campVO = new CampVO();
        campVO.setCampId(camp.getCampId());
        campVO.setCampName(camp.getCampname());
        campVO.setAddress(camp.getAddress());
        campVO.setCityId(camp.getCityId());
        campVO.setIntroduce(camp.getIntroduce());
        campVO.setTel(camp.getTel());
        campRepo.save(campVO);
        return campMapper.updateByPrimaryKeySelective(camp);
    }

    @Override
    public List<City> selectAllCity() throws Exception {
        return cityMapper.selectByExample(null);
    }

    @Override
    public int deleteCamp(Integer campId) throws Exception {
        CampVO campVO = new CampVO();
        campVO.setCampId(campId);
        campRepo.delete(campVO);
        return campMapper.deleteByPrimaryKey(campId);
    }

    @Override
    public List<Camp> selectAllCamp() throws Exception {
        return campMapper.selectByExample(null);
    }

    @Override
    public int insertCamp(Camp camp) throws Exception {
        List<CampVO> campVOS = campMapper.selectAllCampVO();
        campRepo.saveAll(campVOS);
        return campMapper.insert(camp);
    }

    @Override
    public List<CampVO> selectAllCampVO() throws Exception {
        return campMapper.selectAllCampVO();
    }

}
